/**
  ******************************************************************************
  * @file    rtc.c
  * @brief   This file provides code for the configuration
  *          of the RTC instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"

Sys_DataTimeType Sys_Runtime;
Sys_DataTimeType Sys_Adjtime;

/* RTC init function */
void SystemRTC_Config(void)
{
  LL_RTC_InitTypeDef RTC_InitStruct = {0};
	LL_RTC_TimeTypeDef RTC_TimeStruct = {0};
  LL_RTC_DateTypeDef RTC_DateStruct = {0};
	
  /* Peripheral clock enable */
  LL_RCC_EnableRTC();
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTC);

  RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
  RTC_InitStruct.AsynchPrescaler = 127;
  RTC_InitStruct.SynchPrescaler = 255;
  LL_RTC_Init(RTC, &RTC_InitStruct);
	
	if(LL_RTC_BKP_GetRegister(TAMP, LL_RTC_BKP_DR0) != 0x88888888u)
	{
		RTC_TimeStruct.TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
		RTC_TimeStruct.Hours = 0x15;
		RTC_TimeStruct.Minutes = 0x49;
		RTC_TimeStruct.Seconds = 0x40;
		LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
		
		RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_WEDNESDAY;
		RTC_DateStruct.Month = LL_RTC_MONTH_OCTOBER;
		RTC_DateStruct.Day = 0x13;
		RTC_DateStruct.Year = 0x21;
		LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_DateStruct);
		
		LL_RTC_BKP_SetRegister(TAMP, LL_RTC_BKP_DR0, 0x88888888u);
	}
}

void Sys_GetDataTime(void)
{
	uint32_t time = 0;  //date = 0,
//	date = LL_RTC_DATE_Get(RTC);
	time = LL_RTC_TIME_Get(RTC);

//	Sys_Runtime.year   = __LL_RTC_CONVERT_BCD2BIN((uint8_t)(date & 0x000000FF));
//	Sys_Runtime.month  = __LL_RTC_CONVERT_BCD2BIN((uint8_t)((date >> 8) & 0x000000FF));
//	Sys_Runtime.day    = __LL_RTC_CONVERT_BCD2BIN((uint8_t)((date >> 16) & 0x000000FF));
	Sys_Runtime.second = __LL_RTC_CONVERT_BCD2BIN((uint8_t)(time & 0x000000FF));
	Sys_Runtime.minute = __LL_RTC_CONVERT_BCD2BIN((uint8_t)((time >> 8) & 0x000000FF));
	Sys_Runtime.hour   = __LL_RTC_CONVERT_BCD2BIN((uint8_t)((time >> 16) & 0x000000FF));
	Sys_Runtime.allsec = ((Sys_Runtime.hour * 60 * 60) + (Sys_Runtime.minute * 60) + Sys_Runtime.second);
}

void Sys_SetDataTime(void)
{
	LL_RTC_TimeTypeDef RTC_TimeStruct = {0};
//	LL_RTC_DateTypeDef RTC_DateStruct = {0};
	
	RTC_TimeStruct.TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
	RTC_TimeStruct.Hours      = Sys_Adjtime.hour;
  RTC_TimeStruct.Minutes    = Sys_Adjtime.minute;
  RTC_TimeStruct.Seconds    = Sys_Adjtime.second;
  LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, &RTC_TimeStruct);
	
//  RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_FRIDAY;
//	RTC_DateStruct.Month   = Sys_Adjtime.month;
//  RTC_DateStruct.Day     = Sys_Adjtime.day;
//  RTC_DateStruct.Year    = Sys_Adjtime.year;
//  LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BIN, &RTC_DateStruct);
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
